package org.openkoala.koala.wizard.wizardpage.component; 


import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.openkoala.koala.widget.Module;

/**
 * 添加模块依赖对话框
 * @author zyb
 *
 */
@Deprecated
public class AddModuleDependenciesDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	
	// 保存选中后的模块依赖
	private List<String> dependencies = new ArrayList<String>();
	
	private AddModuleDialog addModuleDialog;

	// 保存父项目下的所有模块 
	private List<Module> modules = new ArrayList<Module>();
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AddModuleDependenciesDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		initControlState();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	private void initControlState() {
		Module module = addModuleDialog.getModule();
		if (addModuleDialog.getProject() != null) {
			modules.addAll(addModuleDialog.getProject().getModule());
		} 
		
		// 遍历父项目的子模块，并把模块名称放到Table控件中
		for (Module currentModule : modules) {
			if (module == null || !currentModule.getModuleName().equalsIgnoreCase(module.getModuleName())) { // 在编辑子模块的时候，不能自己添加自己为模块依赖
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(currentModule.getModuleName());
			}
		}
		
		for (String name : addModuleDialog.getDependencyModuleList()) {
			for (int i = 0; i < table.getItemCount(); i++) {
				if (name.equalsIgnoreCase(table.getItem(i).getText())) {
					table.getItem(i).setChecked(true); // 使其选中
					continue;
				}
			}
		}
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.CLOSE | SWT.MAX | SWT.RESIZE | SWT.TITLE);
		shell.setSize(492, 533);
		shell.setLayout(new GridLayout(3, false));
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("模块依赖列表：");
		
		table = new Table(shell, SWT.CHECK | SWT.MULTI);
		GridData gd_table = new GridData(SWT.FILL, SWT.TOP, true, true, 2, 1);
		gd_table.widthHint = 362;
		gd_table.heightHint = 418;
		table.setLayoutData(gd_table);
		
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		
		Composite composite = new Composite(shell, SWT.NONE);
		GridData gd_composite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_composite.widthHint = 330;
		composite.setLayoutData(gd_composite);
		
		Button cancelBtn = new Button(composite, SWT.NONE);
		
		// 取消
		cancelBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		
		cancelBtn.setBounds(250, 10, 80, 27);
		cancelBtn.setText("取消");
		
		Button okBtn = new Button(composite, SWT.NONE);
		
		// 确定
		okBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for (int i = 0; i < table.getItemCount(); i++) {
					if (table.getItem(i).getChecked()) {
						dependencies.add(table.getItem(i).getText());
					}
				}
//				addModuleDialog.updateDependenciesList(dependencies);
				shell.dispose();
			}
		});
		
		okBtn.setBounds(164, 10, 80, 27);
		okBtn.setText("确定");
	}

	public void setAddModuleDialog(AddModuleDialog addModuleDialog) {
		this.addModuleDialog = addModuleDialog;
	}

}
